package com.itheima.nio.tomcat.entry;

import com.itheima.nio.tomcat.domain.HttpRequest;
import com.itheima.nio.tomcat.domain.HttpResponse;
import com.itheima.nio.tomcat.handler.AcceptHandler;
import com.itheima.nio.tomcat.inter.ParseServletConfig;
import com.itheima.nio.tomcat.parse.PropertiesParseServletConfig;
import com.itheima.nio.tomcat.process.DynamicResourceProcess;
import com.itheima.nio.tomcat.runnable.LoaderResourceRunnable;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;

public class HttpServer {

    public static void main(String[] args) throws IOException {

        new Thread(new LoaderResourceRunnable()).start();
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(80));
        serverSocketChannel.configureBlocking(false);
        Selector selector = Selector.open();
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        while (true){
            int select = selector.select();
            if (select != 0){
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                Iterator<SelectionKey> iterator = selectionKeys.iterator();
                while (iterator.hasNext()){
                    SelectionKey selectionKey = iterator.next();
                    if (selectionKey.isAcceptable()){
//                        ServerSocketChannel serverSocketChannel1 = (ServerSocketChannel) selectionKey.channel();
//                        SocketChannel socketChannel = serverSocketChannel1.accept();
//                        socketChannel.configureBlocking(false);
//                        socketChannel.register(selector, SelectionKey.OP_READ);
                        AcceptHandler acceptHandler = new AcceptHandler();
                        SocketChannel socketChannel = acceptHandler.connectSocketChannel(selectionKey);
                    } else if (selectionKey.isReadable()){
                        HttpRequest httpRequest = new HttpRequest(selectionKey);
                        httpRequest.parse();
                        if ("".equals(httpRequest.getRequestURI()) || httpRequest.getRequestURI() == null){
                            selectionKey.cancel();
                            continue;
                        }
                        System.out.println("数据解析完成.....");
                        String requestURI = httpRequest.getRequestURI();
                        HttpResponse httpResponse = new HttpResponse(selectionKey);
                        httpResponse.setHttpRequest(httpRequest);
                        if (requestURI.startsWith("/servlet")){
                            DynamicResourceProcess dynamicResourceProcess = new DynamicResourceProcess();
                            dynamicResourceProcess.process(httpRequest,httpResponse);
                        } else {
                            httpResponse.sendStaticResource();
                        }
                    }
                    iterator.remove();
                }
            }
        }

    }
}
